﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Domain.IRepository.SystemManage;
using ZFine.Repository.SystemManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ZFine.Code;
using ZFine.Domain.Entity;

namespace ZFine.Application.SystemManage
{
    public class ItemsApp
    {
        private IItemsRepository service = new ItemsRepository();


        public ItemsEntity GetFormByCode(string F_Code)
        {
            if (!string.IsNullOrWhiteSpace(F_Code))
            {
                return service.FindEntity(f => f.F_EnCode == F_Code);
            }
            return service.IQueryable().FirstOrDefault();
        }

        public List<ItemsEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public List<ItemsEntity> GetList(string keyword,string childrenid=null)
        {
            var expression = ExtLinq.True<ItemsEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_FullName.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(childrenid)) {
                expression = expression.And(t => t.F_ParentId == childrenid);
            }
            return service.IQueryable(expression).ToList();

        }
        public ItemsEntity GetForm(Expression<Func<ItemsEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).FirstOrDefault();
        }
        public ItemsEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            if (service.IQueryable().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            {
                throw new Exception("删除失败！操作的对象包含了下级数据。");
            }
            else
            {
                service.Delete(t => t.F_Id == keyValue);
            }
        }
        public void SubmitForm(ItemsEntity itemsEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                itemsEntity.Modify(keyValue);
                service.Update(itemsEntity);
            }
            else
            {
                itemsEntity.Create();
                service.Insert(itemsEntity);
            }
        }
    }
}
